package control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cks=req.getCookies();
        if (cks!=null)
        for (Cookie c:cks){
            System.out.println(c.getName()+" "+c.getValue());
        }
        HttpSession session=req.getSession();
        System.out.println("sessionid"+session.getId());
        String name=req.getParameter("usr");
        String pwd=req.getParameter("pwd");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out=resp.getWriter();
        if (name.equals("admin")&&pwd.equals("123")){
            Cookie u=new Cookie("usr","admin");
            //u.setMaxAge(24*60*60);//保存时间
            Cookie p=new Cookie("password","123");
            //p.setMaxAge(24*60*60);
            resp.addCookie(u);
            resp.addCookie(p);
            session.setAttribute("isLogin",true);
            out.write("成功！");
        }
        else {
            out.write("失败！");
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }
}
